//
//  TableViewSomeCellsViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources

//单元格类型
enum SectionItem {
    case TitleButtonSectionItem(title: String, enabled: Bool)
    case TitleSwitchSectionItem(title: String, enabled: Bool)
}
//自定义Section
struct MySection2 {
    var header: String
    var items: [SectionItem]
}
extension MySection2: SectionModelType {
    typealias Item = SectionItem
     
    init(original: MySection2, items: [Item]) {
        self = original
        self.items = items
    }
}


class TableViewSomeCellsViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "32.UITableView的使用6：不同类型的单元格混用"
        view.backgroundColor = .white
        
        test()
    }
    
    func test() {
        
        tableView.register(UINib(nibName: "TitleSwitchTableViewCell", bundle: nil), forCellReuseIdentifier: "titleSwitchCell")
        tableView.register(UINib(nibName: "TitleButtonTableViewCell", bundle: nil), forCellReuseIdentifier: "titleButtonCell")
        
        //初始化数据
        let sections = Observable.just([
            MySection2(header: "我是第一个分区", items: [
                .TitleButtonSectionItem(title: "图片数据1", enabled: true),
                .TitleButtonSectionItem(title: "图片数据2", enabled: true),
                .TitleSwitchSectionItem(title: "开关数据1", enabled: true)
                ]),
            MySection2(header: "我是第二个分区", items: [
                .TitleSwitchSectionItem(title: "开关数据2", enabled: false),
                .TitleSwitchSectionItem(title: "开关数据3", enabled: false),
                .TitleButtonSectionItem(title: "图片数据3", enabled: false)
                ])
            ])
         
        //创建数据源
        let dataSource = RxTableViewSectionedReloadDataSource<MySection2>(
            //设置单元格
            configureCell: { dataSource, tableView, indexPath, item in
                switch dataSource[indexPath] {
                case let .TitleButtonSectionItem(title, enabled):
                    let cell = tableView.dequeueReusableCell(withIdentifier: "titleButtonCell",
                                                             for: indexPath) as! TitleButtonTableViewCell
                    cell.titleL.text = title
                    cell.button.isEnabled = enabled
                    return cell
                     
                case let .TitleSwitchSectionItem(title, enabled):
                    let cell = tableView.dequeueReusableCell(withIdentifier: "titleSwitchCell",
                                                             for: indexPath) as! TitleSwitchTableViewCell
                    cell.titleL.text = title
                    cell.switchx.isOn = enabled
                    return cell
                }
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            }
        )
         
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
    }

}
